require.config({
	shim: {	　　　　　　
		"bootstrap": {	// bootstrap依赖jquery库
			deps: ['jquery'],
			exports: 'bootstrap'
		},
		'handlebars': {　　　　　　　　
			exports: 'Handlebars'　　　　　　
		}　　　　
	},
	paths: {　　　　　　
		//工具模块
		'less': 'tools/less.min',
		'jquery': 'tools/jquery.min',
		'bootstrap': 'tools/bootstrap.min',
		'handlebars': 'tools/handlebars',

		//配置模块
		'config': './config',
		"appConfig":"./appConfig",

		'top_bar': 'modules/top_bar',
		'problem': 'modules/problem',
		'ask': 'modules/ask',
		'modal_template': 'modules/modal_template',
		'cookie': 'modules/cookie',
		'check_log': 'modules/check_log',
		'login': 'modules/login',
		'other_log': 'modules/login',
		'reg': 'modules/reg',
		'user': 'modules/user',　　
	}
});
/************************************header***************************************/
//加载顶栏top_bar模块
require(['top_bar'], function(top_bar){
	top_bar();
});
/************************************content***************************************/
require(['jquery','handlebars','config','cookie','ask'], function ($,Handlebars,config,cookie,ask){
	var studioId,url;
	url=window.location.search;
	studioId=url.substring(url.indexOf("=")+1);
	function handlebarsRender(tplSelector, destSelector, data) {
		//用jquery获取模板
		var source = $(tplSelector).html();
		//预编译模板
		var template = Handlebars.compile(source);
		//匹配json内容
		var html = template(data);
		//输入模板
		$(destSelector).html(html);
	}
	Handlebars.registerHelper("firstInRow",function(index,options){
		if(index%3==0){//每行3列
			return options.fn(this);
		}else{
			return options.inverse(this);
		}
	});//注册一个用于判断是否是某行的第一个item的helper

	Handlebars.registerHelper('if_empty', function(value, options){
		if(value == ''){
			return options.fn(this);
		}
		else{
			return options.inverse(this);
		}
	});	//定义helper，判断头像是否为空
	//获取基础数据
	function fetchStudioDataById(id){
		$.ajax({
			url:config.api.getStudioInfo[0].replace(/\<\w+\>/,id),
			type:config.api.getStudioInfo[1],
			success:function(data){
				handlebarsRender("#basic-template",".contentContainer",data);
				//与基础页面相关的事件绑定应该放在这里
				$(".tabs").delegate("li","click",function(){
					var ref=$(this).data("ref");
					$(".tab-nav").removeClass("active");
					$(this).addClass("active");
					$(".tabs-body").removeClass("show").parent().find(".tabs-body[data-ref="+ref+"]").addClass("show");
				});	
				$(".followStudioBtn").on("click",subscribeStudio);
				ask();//提问相关事件
				getStudioWorksById(id);
				getStudioRecruitById(id);
				getStudioVideosById(studioId);
				getStudioTeachersById(studioId);
			}
		});
	}
	//获取画室作品
	function getStudioWorksById(id){
		$.ajax({
			url:config.api.getStudioWorks[0].replace(/\<\w+\>/,id),
			type:config.api.getStudioWorks[1],
			success:function(data){
				handlebarsRender("#works-template","#works",data);

			}
		});
	}
	//获取画室全部视频
	function getStudioVideosById(id){
		$.ajax({
			url:config.api.getStudioVideos[0].replace(/\<\w+\>/,id),
			type:config.api.getStudioVideos[1],
			success:function(data){
				console.log(data);
				handlebarsRender("#video-template","#videos",data);
				$(".videolist").delegate("li","click",function(){
					var videoId=$(this).data("videoid");
					getOneVideo(videoId);
				});
			}
		});
	}
	//获取某个视频详细信息
	function getOneVideo(videoId){
		$.ajax({
			url:config.api.getOneVideo[0].replace(/\<\w+\>/,videoId),
			type:config.api.getOneVideo[1],
			success:function(data){
				console.log(data);
				handlebarsRender("#videoModal-template","#videoModal",data);
				$(".closeBtn").on("click",function(){
					var player=document.getElementById("player");
					player.pause();
					//停止播放
				});
			}
		});
	}
	//获取画室招聘信息
	function getStudioRecruitById(id){
		$.ajax({
			url:config.api.getStudioRecruit[0].replace(/\<\w+\>/,id),
			type:config.api.getStudioRecruit[1],
			success:function(data){
				handlebarsRender("#recuitSection-template",".recruitBoard",data);

			}
		});
	}
	//获取画室教师
	function getStudioTeachersById(id){
		$.ajax({
			url:config.api.getStudioTeachers[0].replace(/\<\w+\>/,id),
			type:config.api.getStudioTeachers[1],
			success:function(data){
				console.log(data);
				handlebarsRender("#teacherList-template","#teachers",data);

			}
		});
	}
	//订阅该画室
	function subscribeStudio(){
		//var studioId=
		var userId=cookie.getCookie("userId");
		if(userId!=""){
			$.ajax({
				url:config.api.subscribeStudio[0].replace(/\<\w+\>/,userId),
				type:config.api.subscribeStudio[1],
				data:{
					studioId:studioId
				},
				success:function(){
					alert("订阅成功");
				}
			});
		}else{
			alert("请先登录");
		}

	}
	fetchStudioDataById(studioId);

});
require(['less','bootstrap'],function(){
	console.log("less,bootstrap loaded");
});
